package com.kungfuhacking.wristbandpro.location.presenter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.location.bean.OldEntity;

import java.util.List;

/**
 * 选择老人的adapter
 * Created by Aaron on 17/5/3.
 */
public class PickOldAdapter extends RecyclerView.Adapter<PickOldAdapter.MyViewHolder> {

    private Context context;
    private List<OldEntity> oldEntities;

    public PickOldAdapter(Context context, List<OldEntity> oldEntities) {
        this.context = context;
        this.oldEntities = oldEntities;
    }

    @Override
    public PickOldAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PickOldAdapter.MyViewHolder(
                LayoutInflater.from(context).inflate(R.layout.item_pick_old, parent,
                        false));
    }

    @Override
    public void onBindViewHolder(PickOldAdapter.MyViewHolder holder, int position) {

        holder.rl_child.setSelected(oldEntities.get(position).isSelect());
        holder.tv_receiver_name.setText(oldEntities.get(position).getName());

        if (oldEntities.get(position).getDevice() != null)
            holder.tv_watch_tel.setText(oldEntities.get(position).getDevice().getPhoneNum());

        if (oldEntities.get(position).getDevice() != null)
            holder.tv_attention_people.setText(oldEntities.get(position).getDevice().isOnline() ? "在线" : "离线");

        holder.iv_receiver_pic.setImageURI(Uri.parse(
                TextUtils.isEmpty(oldEntities.get(position).getPicUrl()) ? "" : oldEntities.get(position).getPicUrl()));
    }

    @Override
    public int getItemCount() {
        return oldEntities.size();
    }

    public void setOldEntities(List<OldEntity> oldEntities) {
        this.oldEntities = oldEntities;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        SimpleDraweeView iv_receiver_pic;
        LinearLayout rl_child;
        TextView tv_receiver_name;
        TextView tv_watch_tel;
        TextView tv_attention_people;

        MyViewHolder(View view) {
            super(view);
            iv_receiver_pic = (SimpleDraweeView) view.findViewById(R.id.iv_receiver_pic);
            rl_child = (LinearLayout) view.findViewById(R.id.rl_child);
            tv_receiver_name = (TextView) view.findViewById(R.id.tv_receiver_name);
            tv_watch_tel = (TextView) view.findViewById(R.id.tv_watch_tel);
            tv_attention_people = (TextView) view.findViewById(R.id.tv_attention_people);

        }
    }
}